package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import com.sun.org.apache.bcel.internal.generic.DUP;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.print.attribute.standard.Sides;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private StringRedisTemplate template;
    @Override
    public void increaseNumview(String sid, int num) {
        //拼接vokey
       /* String vokey = RedisKeys.STRATEGY_STATIC_VO.join(sid);
        //1.查询redis中vokey是否存在，如果没有，创建vo对象，并且初始化，如果有，直接获取vo对象
        StrategyStatisVO vo = null;
        if(!template.hasKey(vokey)){
            //  - 初始化vo对象时，需要获取MongoDB的sid对应的攻略文章的5个统计数据
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);

            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
        }else {
            //vokey存在,直接获取
            String voStr = template.opsForValue().get(vokey);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
        }*/
        StrategyStatisVO vo = this.getStrategyStaticVo(sid);
        //- 获取vo对象的viewnum属性执行+1操作
        vo.setViewnum(vo.getViewnum()+num);
       this.setStrategyStaticVo(vo);
    }

    @Override
    public StrategyStatisVO getStrategyStaticVo(String sid) {
        String vokey = RedisKeys.STRATEGY_STATIC_VO.join(sid);
        StrategyStatisVO vo = null;
        if(!template.hasKey(vokey)){
            //不存在，初始化
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
            //更新
            template.opsForValue().set(vokey,JSON.toJSONString(vo));
        }else {
            String voStr = template.opsForValue().get(vokey);
            vo = JSON.parseObject(voStr,StrategyStatisVO.class);
        }
        return vo;
    }

    @Override
    public void setStrategyStaticVo(StrategyStatisVO vo) {
        String vokey = RedisKeys.STRATEGY_STATIC_VO.join(vo.getStrategyId());
        //- 更新redis中的vo对象
        template.opsForValue().set(vokey,JSON.toJSONString(vo));
    }

    @Override
    public void increaseReplynum(String sid, int num) {

        //- 评论成功之后，执行评论+1逻辑
        //- 通过评论的攻略sid 拼接vokey=strategy_static_vo:sid
        StrategyStatisVO vo = this.getStrategyStaticVo(sid);

        //- 获取评论数并+1，然后设置到vo对象replynum属性中
        vo.setReplynum(vo.getReplynum()+1);
        /*String vokey = RedisKeys.STRATEGY_STATIC_VO.join(sid);
        //- 更新vo对象
        template.opsForValue().set(vokey,JSON.toJSONString(vo));*/
        this.setStrategyStaticVo(vo);
    }

    @Override
    public StrategyStatisVO increaseFavornum(String sid, String uid) {
        //获取用户收藏列表集合
        StrategyStatisVO vo = this.getStrategyStaticVo(sid);
        String keys = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        if(template.hasKey(keys)){
            String sidStr = template.opsForValue().get(keys);

        }
        return null;
    }

    @Override
    public boolean favor(String sid, String uid) {
        List<String> uidList = new ArrayList<>();
        //- 请求进入收藏接口，要获取攻略sid，用户uid
        //- 使用uid拼接出用户攻略收藏列表key
        String keyList = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //  - key=user_favor:uid
        //- 判断key是否存在
        if(template.hasKey(keyList)) {
            //- 如果存在：通过key获取用户攻略id收藏uidList集合
            final String voStr = template.opsForValue().get(keyList);
            uidList = JSON.parseArray(voStr,String.class);
        }
        //- 如果不存在：直接new一个uidList集合
        //- 判断uidList集合是否包含sid
        StrategyStatisVO vo = this.getStrategyStaticVo(sid);
        if(uidList.contains(sid)) {
            //- 如果包含，表示当前请求是取消收藏操作，获取vo对象，将vo对象中favornum数-1，将sid移除出uidList集合
            vo.setFavornum(vo.getFavornum()-1);
            uidList.remove(sid);
        }else {
            //- 如果不包含，表示当前请求是收藏操，作获取vo对象，将vo对象中favornum数+1，将sid添加到uidList集合中
            vo.setFavornum(vo.getFavornum()+1);
            uidList.add(sid);
        }
        //- 更新uidList集合，更新vo对象
        template.opsForValue().set(keyList,JSON.toJSONString(uidList));
        this.setStrategyStaticVo(vo);
        return uidList.contains(sid);
    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {
        //拼接key
        //- 使用uid，sid拼接标记的key：
        //  strategy_thumb:uid:sid
        String key = RedisKeys.STRATEGY_THUMB.join(uid, sid);

        //- 判断可以是否存在，存在，表示今天已经顶过，直接返回，不做任何操作
        if(template.hasKey(key)){
            return false;
        }

        //- key不存在，将key缓存redis中，value值任意，设置有效时间为：当前时间到今天最后一秒，获取vo对象，将点赞数+1
        StrategyStatisVO vo = this.getStrategyStaticVo(sid);
        vo.setThumbsupnum(vo.getThumbsupnum()+1);
        //设置有效时间时间
        Date now = new Date();
        Date end = DateUtil.getEndDate(now);
        long time = DateUtil.getDateBetween(now, end);

        template.opsForValue().set(key,"1", time,TimeUnit.SECONDS);

        //- 更新vo对象
        this.setStrategyStaticVo(vo);

        return true;
    }

    @Override
    public boolean isVoExists(String sid) {
        String key = RedisKeys.STRATEGY_STATIC_VO.join(sid);
        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStrategyVoByPattern(String pattern) {
        List<StrategyStatisVO> list = new ArrayList<>();
        String p = RedisKeys.STRATEGY_STATIC_VO.join(pattern);
        //根据前缀查询所有vo对象的key
        //keys strategy_static_vo:*
        Set<String> keys = template.keys(p);
        if(keys != null && keys.size() > 0){
            for (String key : keys) {
                String voStr = template.opsForValue().get(key);
                list.add(JSON.parseObject(voStr,StrategyStatisVO.class));
            }
        }
        return list;
    }
}
